import React from 'react'
import {
  Divider,
  Card,
  Modal,
  Input,
  Table,
  Tabs
} from 'antd'
import moment from 'moment'
import DescriptionList from 'components/DescriptionList'
import styles from './index.less'
import { sourceSystem, productType, contractOrderType, quoteType, purchaseType } from 'utils/enumConfig'
import { LwjDownload } from 'components'
import { LwjPic4Absolute } from 'components'

const { Description } = DescriptionList
const TabPane = Tabs.TabPane

const ContractDetailModal = ({
  currentContractDetail = {},
  showOrderQuote,
  ...contractDetailModalProps
}) => {

  const isDoorDesign = currentContractDetail.productType === 'DOORPLANK';
  let loaded = _.isEmpty(currentContractDetail)

  const orderContractAt = moment(currentContractDetail.orderContractAt).format('YYYY-MM-DD HH:mm:ss') === 'Invalid date' ? '暂无时间' : moment(currentContractDetail.orderContractAt).format('YYYY-MM-DD HH:mm:ss')
  const installContractAt = moment(currentContractDetail.installContractAt).format('YYYY-MM-DD HH:mm:ss') === 'Invalid date' ? '暂无时间' : moment(currentContractDetail.installContractAt).format('YYYY-MM-DD HH:mm:ss')
  const measuredAt = moment(currentContractDetail.measuredAt).format('YYYY-MM-DD HH:mm:ss') === 'Invalid date' ? '暂无时间' : moment(currentContractDetail.measuredAt).format('YYYY-MM-DD HH:mm:ss')

  // 处理操作日志->按照创建时间排序
  const handleLogs = _.sortBy(currentContractDetail.contractOrderActivitySummaries, (log) => {
    return -log.createTime
  })
  //第三方来源系统商品信息
  const sourceSystemDataSource = _.sortBy(currentContractDetail.commodityThirdSummary, (commodity) => {
    return -commodity.createTime
  })

  const logsColumns = [
    {
      title: '操作人',
      dataIndex: 'handlerName',
      key: 'handlerName',
      width: '10%',
      render: (text, record, index) => (record.handlerName)
    }, {
      title: '操作时间',
      dataIndex: 'handleTime',
      key: 'handleTime',
      width: '30%',
      render: (text, record, index) => (moment(record.createTime).format('YYYY-MM-DD HH:mm:ss'))
    }, {
      title: '操作类型',
      dataIndex: 'memo',
      key: 'memo',
      width: '60%',
    }
  ]
  const sourceSystemColumns = [
    {
      title: '商品名称',
      dataIndex: 'name',
      key: 'name',
    },
    {
      title: '商品金额',
      dataIndex: 'amount',
      key: 'amount',
      render: (text, record, index) => (record.amount.toFixed(2))
    },
    {
      title: '商品图片',
      dataIndex: 'imgUrl',
      key: 'imgUrl',
      render: (record) => (
        <LwjPic4Absolute title='安装效果图' fileSrc={record} />
      )
    }, {
      title: '商品说明',
      dataIndex: 'description',
      key: 'description',
    }
    , {
      title: '商品规格',
      dataIndex: 'spec',
      key: 'spec',
    }
    , {
      title: '商品重量',
      dataIndex: 'weight',
      key: 'weight',
    }
    , {
      title: '商品数量',
      dataIndex: 'quantity',
      key: 'quantity',
    }
  ]
  const commodityColumns = [
    {
      title: '商品名称',
      dataIndex: 'name',
      key: 'name',
    },
    // {
    //   title: '商品物料',
    //   dataIndex: 'materiel',
    //   key: 'materiel',
    //   render: (text, record, index) => (
    //     record.materielSnapshotList.map(element => {
    //       return element.materialCode + "*" + element.materialNum + ";"
    //     })
    //   )
    // },
    {
      title: '商品图片',
      dataIndex: 'coverPath',
      key: 'coverPath',
      width: "10%",
      render: (record) => (
        <LwjPic4Absolute title='商品图片' fileSrc={record} />
      )
    }
    // ,
    // {
    //   title: '商品金额',
    //   dataIndex: 'channelPrice',
    //   key: 'channelPrice',
    //   render: (text, record, index) => (record.channelPrice.toFixed(2))
    // }
    , {
      title: '单位',
      dataIndex: 'unitName',
      key: 'unitName',
    }
    , {
      title: '商品数量',
      dataIndex: 'number',
      key: 'number',
    }
    // , {
    //   title: '实付金额',
    //   dataIndex: 'realPrice',
    //   key: 'realPrice',
    //   render: (text, record, index) => ((record.channelPrice * record.number).toFixed(2))
    // }
  ]
  const materielItemsColumns = [
    {
      title: '物料编码',
      dataIndex: 'code',
      key: 'code'
    }, {
      title: '物料名称',
      dataIndex: 'name',
      key: 'name'
    }, {
      title: '物料数量',
      dataIndex: 'quantity',
      key: 'quantity'
    }
  ]
  const installOrArrive = currentContractDetail.contractOrderType === "CUSTOMIZE" ? "合同安装时间" : "合同到货时间";

  return (
    <Modal {...contractDetailModalProps}>
      {!loaded && <Tabs defaultActiveKey="1" type="card">
        <TabPane tab="基础信息" key="1">
          <Card>
            <DescriptionList
              size="small">
              <Description term="合同号">{currentContractDetail.contractNum}</Description>
              <Description term="合同类型">{contractOrderType[currentContractDetail.contractOrderType]}</Description>
              <Description term="产品类型">{productType[currentContractDetail.productType]}</Description>
              <Description term="是否需要设计">{currentContractDetail.designNeeded ? '是' : '否'}</Description>
              <Description term="是否推送丽维家">{currentContractDetail.pushToOMSNeeded ? '是' : '否'}</Description>
              <Description term="采购方式">{purchaseType[currentContractDetail.purchaseType]}</Description>
              <Description term="合同签订时间">{orderContractAt}</Description>
              <Description term={installOrArrive}>{installContractAt}</Description>
              <Description term="客户姓名">{currentContractDetail.customerName}</Description>
              <Description term="客户手机号">{currentContractDetail.customerPhoneNum}</Description>
              <Description term="收货人姓名">{currentContractDetail.consigneeName}</Description>
              <Description term="收货人手机号">{currentContractDetail.consigneePhoneNum}</Description>
              <Description term="收货人地址">
                {!_.isEmpty(currentContractDetail.consigneeProvince)
                  && !_.isEmpty(currentContractDetail.consigneeCity)
                  && !_.isEmpty(currentContractDetail.consigneeDistrict) ? currentContractDetail.consigneeProvince.name
                  + currentContractDetail.consigneeCity.name
                  + currentContractDetail.consigneeDistrict.name
                  + currentContractDetail.consigneeAddress : '无'}
              </Description>
              {!_.isEmpty(currentContractDetail.endOrderAttachment) &&
                <Description term="成品清单">{<LwjDownload title={'点击下载'}
                  targetUrl={currentContractDetail.endOrderAttachment} />}</Description>}
            </DescriptionList>
          </Card>
        </TabPane>
        {!_.isEmpty(currentContractDetail.designAttachment) && <TabPane tab="设计信息" key="2">
          <Card>
            {currentContractDetail.designNeeded && <DescriptionList
              size="small">
              <Description term="测量时间">{measuredAt}</Description>
              <Description term="测量设计师姓名">{currentContractDetail.measureDesignerName}</Description>
              <Description term="测量设计师手机号">{currentContractDetail.measureDesignerPhoneNum}</Description>

              <Description term="测量图纸">{<LwjDownload title={'点击下载'}
                targetUrl={currentContractDetail.measuredPictureAttachment} />}</Description>
              <Description term="现场图片">{<LwjDownload title={'点击下载'}
                targetUrl={currentContractDetail.scenePictureAttachment} />}</Description>
              {!_.isEmpty(currentContractDetail.backupDesignAttachment) && <Description term="原设计方案">{<LwjDownload title={'点击下载'} targetUrl={currentContractDetail.backupDesignAttachment} />}</Description>}
              <Description term="下推设计方案">{<LwjDownload title={'点击下载'} targetUrl={currentContractDetail.designAttachment} />}</Description>
              {currentContractDetail.status === 'DENIED_DESIGN' &&
                <Description term="设计不通过原因">{currentContractDetail.designDeniedReason}</Description>}
            </DescriptionList>}
            {!currentContractDetail.designNeeded && <DescriptionList
              size="small">
              <Description term="商家设计师姓名">{currentContractDetail.designerUserName}</Description>
              <Description term="商家设计师手机号">{currentContractDetail.designerUserPhoneNum}</Description>
              {!_.isEmpty(currentContractDetail.backupDesignAttachment) && <Description term="原设计方案">{<LwjDownload title={'点击下载'}
                targetUrl={currentContractDetail.backupDesignAttachment} />}</Description>}
              <Description term="下推设计方案">{<LwjDownload title={'点击下载'}
                targetUrl={currentContractDetail.designAttachment} />}</Description>
              {currentContractDetail.status === 'DENIED_DESIGN' &&
                <Description term="设计不通过原因">{currentContractDetail.designDeniedReason}</Description>}
            </DescriptionList>}
          </Card>
        </TabPane>}
        {!isDoorDesign && <TabPane tab="商品信息" key="11">
          <Card>
            <DescriptionList
              size="small"
              style={{ marginBottom: 32 }}>
              <Description
                term="来源系统">
                {sourceSystem[currentContractDetail.sourceSystem === null ? "NULL" : currentContractDetail.sourceSystem]}
              </Description>
              <Description
                term="下单备注">
                {currentContractDetail.goodsDescription}
              </Description>
              <Description
                term="来源订单号">
                {currentContractDetail.salesOrderNum}
              </Description>
            </DescriptionList>
            <Table
              style={{ marginBottom: 24 }}
              pagination={false}
              dataSource={currentContractDetail.goodsSnapshots}
              columns={commodityColumns}
              rowKey="createTime" />
          </Card>
        </TabPane>}
        {!_.isEmpty(currentContractDetail.orderQuoteSummary) && showOrderQuote && <TabPane tab="报价信息" key="3">
          <Card>
            <DescriptionList
              size="small"
              style={{ marginBottom: 32 }}>
              <Description
                term="原合同金额">{currentContractDetail.orderQuoteSummary.preOrderAmount.toFixed(2)}</Description>
              <Description
                term="装修返点金额">{currentContractDetail.orderQuoteSummary.decorationRebateAmount.toFixed(2)}</Description>
              <Description
                term="样柜返点金额">{currentContractDetail.orderQuoteSummary.cabinetRebateAmount.toFixed(2)}</Description>
              <Description term="活动优惠金额">{currentContractDetail.orderQuoteSummary.eventsAmount.toFixed(2)}</Description>
              <Description term="合同金额">{currentContractDetail.orderQuoteSummary.orderAmount.toFixed(2)}</Description>
              <Description term="报价备注">{currentContractDetail.orderQuoteSummary.memo}</Description>
              {currentContractDetail.status === 'DENIED_QUOTED' &&
                <Description term="报价不通过原因">{currentContractDetail.orderQuoteSummary.quoteDeniedReason}</Description>}
              <Description term="原报价方案">{<LwjDownload title={'点击下载'} targetUrl={currentContractDetail.orderQuoteSummary.backupQuoteAttachment} />}</Description>
              <Description term="报价方案">{<LwjDownload title={'点击下载'}
                targetUrl={currentContractDetail.orderQuoteSummary.quoteAttachment} />}</Description>
              <Description term={quoteType[currentContractDetail.orderQuoteSummary.deductionType]}>{currentContractDetail.orderQuoteSummary.orderAmount.toFixed(2)}</Description>
            </DescriptionList>
          </Card>
        </TabPane>}
        {!_.isEmpty(currentContractDetail.orderPublishInfoSummary) && !_.isEmpty(currentContractDetail.orderPublishInfoSummary.contractAmount) && <TabPane tab="下单信息" key="4">
          <Card>
            <DescriptionList
              size="small"
              style={{ marginBottom: 32 }}>
              <Description term="合同号">{currentContractDetail.orderPublishInfoSummary.contractNum}</Description>
              <Description term="合同签订时间">
                {currentContractDetail.orderPublishInfoSummary.contractSignedAt > 0 ?
                  moment(currentContractDetail.orderPublishInfoSummary.contractSignedAt).format('YYYY-MM-DD HH:mm:ss') : ''}
              </Description>
              {!_.isEmpty(currentContractDetail.orderPublishInfoSummary.contractInstalledAt) &&
                <Description term="合同安装时间">
                  {moment(currentContractDetail.orderPublishInfoSummary.contractInstalledAt).format('YYYY-MM-DD HH:mm:ss')}
                </Description>}
              {!_.isEmpty(currentContractDetail.orderPublishInfoSummary.contractArrivalAt) &&
                <Description term="合同到货时间">
                  {moment(currentContractDetail.orderPublishInfoSummary.contractArrivalAt).format('YYYY-MM-DD HH:mm:ss')}
                </Description>}
              <Description
                term="合同金额">{currentContractDetail.orderPublishInfoSummary.contractAmount.toFixed(2)}</Description>
              <Description term="收货人姓名">{currentContractDetail.orderPublishInfoSummary.receiptUserName}</Description>
              <Description
                term="收货人手机号">{currentContractDetail.orderPublishInfoSummary.receiptUserPhoneNum}</Description>
              <Description term="收货人地址">{currentContractDetail.orderPublishInfoSummary.receiptAddress}</Description>
              {currentContractDetail.contractOrderType === 'CUSTOMIZE' &&
                <Description term="CAD图纸">{<LwjDownload title={'点击下载'}
                  targetUrl={currentContractDetail.orderPublishInfoSummary.cadAttachment} />}</Description>}
              {currentContractDetail.contractOrderType === 'END' &&
                <Description term="成品订单附件">{<LwjDownload title={'点击下载'}
                  targetUrl={currentContractDetail.orderPublishInfoSummary.endOrderAttachment} />}</Description>}
            </DescriptionList>
            {currentContractDetail.contractOrderType === 'END' &&
              <div>
                <Divider style={{ marginBottom: 32 }} />
                <div className={styles.title}>物料信息</div>
                <Table
                  style={{ marginBottom: 24 }}
                  pagination={false}
                  dataSource={currentContractDetail.orderPublishInfoSummary.materielItems}
                  columns={materielItemsColumns}
                  rowKey="code" />
              </div>}
          </Card>
        </TabPane>}
        {currentContractDetail.sourceOrderNum && currentContractDetail.sourceSystem !== "SAlES_MARKET" && currentContractDetail.sourceSystem !== "WX_LITE" && <TabPane tab="第三方记录" key="6">
          <Card>
            <DescriptionList
              size="small"
              style={{ marginBottom: 32 }}>
              <Description
                term="来源系统">
                {sourceSystem[currentContractDetail.sourceSystem === null ? "NULL" : currentContractDetail.sourceSystem]}
              </Description>
              <Description
                term="第三方订单号">
                {currentContractDetail.sourceOrderNum}
              </Description>
            </DescriptionList>
            <Table
              style={{ marginBottom: 24 }}
              dataSource={sourceSystemDataSource}
              columns={sourceSystemColumns}
              rowKey="createTime" />
          </Card>
        </TabPane>}
        <TabPane tab="操作记录" key="5">
          <Table
            style={{ marginBottom: 24 }}
            pagination={false}
            dataSource={handleLogs}
            columns={logsColumns}
            rowKey="createTime" />
        </TabPane>
      </Tabs>}
    </Modal>
  )
}

export default ContractDetailModal
